<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>站点列表</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.17.1/bootstrap-table.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.17.1/bootstrap-table.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.17.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="../../configure.js"></script>
    <script src="../layer/layer.js"></script>
    <style>
        .prompt{
            background-color: brown;
            color: #fff;
        }
    </style>
</head>
<body>
<div>
    <div id ="table">
    </div>
</div>
<div class="container text-center">
    <input type="button" id="deleteAll" value="批量删除" class=" btn btn-primary">
    <input id="first" type="button" value="首页" class="btn btn-primary">
    <input id="pre" type="button" value="上一页" class="btn btn-primary">
    <span id="page">
    </span>
    <span id="pageNow">1</span>
    <span id="pages">1</span>
    <input id="next" type="button" value="下一页" class="btn btn-primary">
    <input id="last" type="button" value="尾页" class="btn btn-primary">
</div>
</div>
<script src="../js/jquery-3.3.1.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script>
    $("#pageNow").hide()
    $("#pages").hide()

    //let index = $(".div-page .showPage").index(this);
    //let pageId = $(".none").find(".information").eq(index).find(".pagId").text();

    $("#deleteAll").click(function(){
        
        // boxIds.push(12182);
        // boxIds.push(12183);
        // boxIds.push(12184);
        var spCodesTemp = "";
        var ids = "";
        $('input:checkbox[name=checkbox]:checked').each(function(i){
            var index = $(".checkbox").index(this);
            var siteId =$("#table .table tr").eq(index+1).find(".siteId").text();
            ids += siteId+","
            // if(0==i){
            //     spCodesTemp = $(".checkbox").index(this);
            // }else{
            //     spCodesTemp += (","+$(".checkbox").index(this));
            // }
        });
        console.log(ids)
        $.ajax({
            url: url + "/admin/adminSiteDeleteAll",
            async: false,
            dataType: "json",
            type: "post",
            data : {
                ids:ids
            },
            success: function (data) {
                if(1 == data.code){
                    layer.msg("删除成功")
                }else if(2 == data.code){
                    layer.msg("删除失败")
                }
            }
        }, "json")
        list();
    })

    $(document).on("click",".deleteOne",function(){
        let index =$("#table .table .deleteOne").index(this);
        let siteId =$("#table .table tr").eq(index+1).find(".siteId").text();
        console.log(index)
        console.log(siteId);

        $.ajax({
            url: url + "/admin/adminSiteDelete",
            async: false,
            dataType: "json",
            type: "post",
            data : {
                siteId:siteId
            },
            success: function (data) {
                if(1 == data.code){
                    layer.msg("删除成功")
                }else if(2 == data.code){
                    layer.msg("删除失败")
                }
            }
        }, "json")

        list();
    })

    $(document).on("click","#checkAll",function(){
        if($(this).prop("checked")){
           
            $(".checkbox").prop("checked",true)
        }else{
           
            $(".checkbox").prop("checked",false)
        }
    })

    $(document).on("click",".checkbox",function(){
        if($(this).prop("checked")){
           
            // $("#checkAll").prop("checked",true)
        }else{
           
            $("#checkAll").prop("checked",false)
        }
    })

    function list(){
        let cont=$("#pageNow").text()
        $.post(url+"/admin/adminSiteList",{
            pageNum:cont
        },function(data){
            $("#pages").html(data.data.pages)
            let html = ' ' +
                '<table class="table  table-hover table-bordered ">' +
                '       <td><input id="checkAll" type="checkbox">&emsp;&emsp;全选</td>' +
                '       <td>站点ID</td>' +
               
                '       <td>站点名</td>' +
                '       <td>手机号</td>' +
                '       <td>省</td>' +
                '       <td>市</td>' +
             
                '       <td>操作</td>'
                ''
            let htmlPage =''
            let list = data.data.list
            if(data.data.pages<10){
                for(let i = 0; i< data.data.pages;i++){
                    htmlPage += `
                <button class="page-a" href="#">`+Number(i+1)+`</button>
               `
                }
            }
            if(data.data.pages>10){
                if(cont>5){
                  
                    for(let i = Number(cont) -Number(5); i< Number(5)+Number(cont);i++){
                        htmlPage += `
                            <button class="page-a" href="#">`+Number(i+1)+`</button>
                        `
                    }
                    cont = 5;
                }else{
                    for(let i = 0; i< 10;i++){
                        htmlPage += `
                            <button class="page-a" href="#">`+Number(i+1)+`</button>
                        `
                    }
                }
                
            }


            $("#page").html(htmlPage)
            for(let i = 0; i< list.length;i++){
                html += `
               <tr><td><input class="checkbox" name="checkbox" type="checkbox"></td>
                    <td class="siteId">${list[i].siteId}</td>
                    <td>${list[i].siteName}</td>
                    <td>${list[i].sitePhone}</td>
                    <td>${list[i].siteProvince}</td>
                    <td>${list[i].siteCity}</td>
                  
                    <td><a class="deleteOne">删除</a></td>
                   </tr>`
            }

            html += `</table>`
            $("#table").html(html)

            $("#page").find(".page-a").eq(cont-1).addClass("prompt");
            
           
        });
    }
    window.onload = list;
    $("#next").click(function () {
        let pageNow = $("#pageNow").text()
        let pages = $("#pages").text()
        if(Number(pageNow)<pages){
            $("#pageNow").html(Number(pageNow)+Number(1))
        }
        list();
    })
    $("#pre").click(function () {
        let pageNow = $("#pageNow").text()
        if(Number(pageNow)>1){
            let page = Number(pageNow)-Number(1)
            $("#pageNow").html(page)
        }
        list();
    })

    $("#first").click(function () {
        $("#pageNow").html(1)
        list();
    })

    $("#last").click(function () {
        let pages = $("#pages").text()
        $("#pageNow").html(pages)
        list();
    })

    $(document).on("click",".page-a",function(){
        let pages = $(this).text();
        $("#pageNow").html(pages);
        console.log(pages);
        list();
    })
</script>
</body>
</html>